<template>
  <div class="aus-c">
    <div class="url-view">
      <div id="id-api-url" class="url">{{ urlSel.url ? urlSel.url : '' }}</div>
      <div class="rx-btn rx_no_select" @click="onPopupShow">
        <span>选择:&nbsp;&nbsp;&nbsp;</span>
        <span>{{ urlSel.text ? urlSel.text : '' }}</span>
        <span>></span>
      </div>
    </div>
    <div class="url-popup">
      <div>
        <div class="tip">选择环境</div>
        <div class="aus-c-line"></div>
        <div
          v-for="(item, index) in getUrls"
          :key="'up-' + index"
          class="url-view popup"
          @click="select(item, index, $event)"
        >
          <template v-if="!item.input">
            <div class="url">{{ item.url ? item.url : '' }}</div>
            <div class="rx-btn">{{ item.text ? item.text : '' }}</div>
          </template>
          <template v-else>
            <input
              class="input"
              type="text"
              placeholder="url: http://www http://ip"
              v-model="diyUrl"
            />
            <div class="input text rx_no_select" @click.stop="select(item, 3, $event)">
              <span>确定选择</span>
            </div>
            <input
              class="input"
              type="text"
              placeholder="环境标识: 测试1"
              v-model="diyText"
            />
          </template>
        </div>
        <div v-if="toast" class="toast">
          <span>{{ toast }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import ApiCookiesUtil from "../utils/api-cookies-util"
import apiUtils from "../utils/api-util"
import ApiEleDis from '../utils/api-element-display'

const APIDIYURLKEY = 'diy_url'
const APIDIYURLVALUE = 'diy_value'

export default {
  data() {
    return {
      urlSel: {},
      diyUrl: '',
      diyText: '',
      toast: '',
    };
  },
  created() {
    this.config();
  },
  mounted() {
    const _this = this;
    document.onclick = function (e) {
      _this.setElementStyleDisplay('url-popup', false, e)
    };
  },
  computed: {
    getUrls() {
      const devUrl = apiUtils.dev.url;
      const devText = apiUtils.dev.text;
      const prodUrl = apiUtils.prod.url;
      const prodText = apiUtils.prod.text;
      const diyUrl = ApiCookiesUtil.get(APIDIYURLKEY) || '';
      const diyText = ApiCookiesUtil.get(APIDIYURLVALUE) || '';

      this.diyUrl = diyUrl;
      this.diyText = diyText;
      const urls = [
        { url: devUrl, text: devText, input: false },
        { url: prodUrl, text: prodText, input: false },
        { url: diyUrl, text: diyText, input: true },
      ];
      return urls;
    },
  },
  methods: {
    config() {
      const devUrl = apiUtils.dev.url;
      const devText = apiUtils.dev.text;
      const url = ApiCookiesUtil.get(apiUtils.selUrl) || devUrl;
      const text = ApiCookiesUtil.get(apiUtils.selText) || devText;

      const diyUrl = ApiCookiesUtil.get(APIDIYURLKEY) || '';
      const diyText = ApiCookiesUtil.get(APIDIYURLVALUE) || '';

      this.diyUrl = diyUrl;
      this.diyText = diyText;

      this.urlSel = { url, text };
    },
    select(item, index, event) {
      let url = item.url;
      let text = item.text;
      if (index === 2) {
        return;
      } else if (index === 3) {
        url = this.diyUrl;
        text = this.diyText;
      }

      if (!this.isUrl(url) || !text) {
        this.showToast();
        return;
      }

      this.urlSel = { url, text };
      ApiCookiesUtil.save(apiUtils.selUrl, url);
      ApiCookiesUtil.save(apiUtils.selText, text);
      this.changeContack(false)
      this.stopContack(event)
      this.$emit('reload')
    },
    onPopupShow() {
      this.changeContack(true)
    },
    showToast() {
      this.toast = '自定义环境: 请正确填写'
      setTimeout(() => {
        this.toast = '';
      }, 1500);
    },
    isUrl(url) {
      url = url || '';
      if (url.length === 1 && url === '/') {
        return true
      } else if (url.length === 2 && url === './') {
        return true
      } else if (url.length < 4) {
        return false;
      }
      const prefix = url.substring(0, 4);
      if (prefix !== 'http') {
        return false;
      }
      return true;
    },
    stopContack(e) {
      // 阻止事件向上传递
      if (e.stopPropagation) { // 非IE浏览器
        e.stopPropagation()
      } else if (typeof e.cancelBubble === 'boolean') { // IE浏览器
        e.cancelBubble = true
      }
    },
    changeContack(show) {
      this.setElementStyleDisplay('url-popup', show)
    },
    setElementStyleDisplay(className, show = false, e = null) {
      ApiEleDis.elementRootDisplay({e, className, show, breakClass: 'aus-c'})
    }
  },
};
</script>

<style src="../css-component/style.css"></style>

<style scoped>
.aus-c {
  font-size: 14px;
  font-weight: 400;
  background-color: #eee;
  color: black;
  position: relative;
  border-radius: 4px;
  overflow: visible;
}

.aus-c .url-view {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  overflow: hidden;
}

.aus-c .url-view .url {
  padding-left: 10px;
}

.aus-c .url-popup {
  display: none;
  position: absolute;
  left: 20px;
  right: 0;
  top: 42px;
  padding: 5px;
  background-color: #eee;
  border-radius: 4px;
  box-shadow: 0px 1px 6px 0px rgba(244, 244, 158, 0.4);
  z-index: 10;
}
.aus-c .url-popup .tip {
  margin: 5px 0;
  font-weight: bold;
}
.aus-c .url-view.popup {
  border-top: 1px solid rgb(218, 210, 210);
}

.aus-c .url-popup .input {
  flex: 1;
  height: 30px;
  cursor: pointer;
}

.aus-c .url-popup .input.text {
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.aus-c .url-popup .toast {
  position: absolute;
  left: 20px;
  top: 20px;
  bottom: 20px;
  right: 20px;
  font-size: 25px;
  border-radius: 20px;
  background-color: hotpink;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}
</style>